package com.situ.hotel.controller;

import java.io.IOException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Date;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.situ.hotel.entity.Emp;
import com.situ.hotel.util.JDBCUtil;

// 继承HttpServlet，Servlet和Jsp都是可以在浏览器直接访问
// http://localhost:8080/Hotel/emp
@WebServlet("/emp")
public class EmpServlet extends HttpServlet{

	// 访问Servlet默认就是执行Service方法
	@Override
	protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		// syst
		System.out.println("EmpServlet.service()");
		//  http://localhost:8080/Hotel/emp?method=selectAll
		//  http://localhost:8080/Hotel/emp?method=delete
		String method = req.getParameter("method");
		if (method == null || "".equals(method)) {
			method = "selectAll";
		}
		switch (method) {
		case "selectAll":
			selectAll(req, resp);
			break;
		case "delete":
			delete(req, resp);
			break;

		default:
			break;
		}
		
		
		
	}

	private void delete(HttpServletRequest req, HttpServletResponse resp) throws IOException {
		// http://localhost:8080/Hotel/emp?mehtod=delete&code=5
		String code = req.getParameter("code");
		
		Connection connection = null;
		PreparedStatement statement = null;
		try {
			connection = JDBCUtil.getConnection();
			String sql = "DELETE FROM `emp` WHERE `code`=?";
			statement = connection.prepareStatement(sql);
			// 填上？占位符的值
			statement.setString(1, code);
			// 在执行之前打印statement会打印执行的sql语句
			System.out.println(statement);
			int count = statement.executeUpdate();
			System.out.println("count: " + count);
		} catch (SQLException e) {
			e.printStackTrace();
		}
		
		// 删除完了之后应该跳转到查找所有的界面，把最新的数据查找展示
		resp.sendRedirect(req.getContextPath() + "/emp?method=selectAll");
	}

	private void selectAll(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		Connection connection = null;
		PreparedStatement statement = null;
		ResultSet resultSet = null;
		ArrayList<Emp> list = new ArrayList<>();
		try {
			connection = JDBCUtil.getConnection();
			String sql = "SELECT `id`,`code`,`name`,`birthday`,`address`,`phone`,`password`,`dept_code` FROM `emp`";
			statement = connection.prepareStatement(sql);
			System.out.println(statement);
			resultSet = statement.executeQuery();
			while (resultSet.next()) {
				int id = resultSet.getInt("id");
				String code = resultSet.getString("code");
				String name = resultSet.getString("name");
				String address = resultSet.getString("address");
				String phone = resultSet.getString("phone");
				String password = resultSet.getString("password");
				Date birthday = resultSet.getDate("birthday");
				String deptCode = resultSet.getString("dept_code");
				Emp emp = new Emp(id, code, name, birthday, address, phone, password, deptCode);
				list.add(emp);
			}
			
			for (Emp emp : list) {
				System.out.println(emp);
			}
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			JDBCUtil.close(connection, statement, resultSet);
		}
		
		// 把list集合数据放到request内存里面保存
		req.setAttribute("list", list);
		// 跳转到emp_list.jsp界面展示数据
		req.getRequestDispatcher("/emp_list.jsp").forward(req, resp);
	}
}
